package com.zzx.project.controller;

import com.zzx.project.entity.Leavingmessage;
import com.zzx.project.entity.User;
import com.zzx.project.service.LmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

//留言Controller
@Controller
@RequestMapping("lm")
public class LmController {

    @Autowired
    LmService lmService;

    //添加留言
    @RequestMapping("add")
    public String addMessage(Leavingmessage leavingmessage, HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        leavingmessage.setLmcreateby(user.getUserid());
        int flag = lmService.addMessage(leavingmessage);
        //添加成功
        if (flag > 0) {
            session.setAttribute("LeavingMessages", lmService.getMessagesById(user.getUserid()));
        } else {
            return "error/5xx";
        }
        Object visitor = model.getAttribute("visitor");
        //留言接收人与登录者不一致的话转为访客模式进入接收人信息页
        if (leavingmessage.getLmto() != user.getUserid()) {
            Object uid = model.getAttribute("uid");
            return "redirect:/page/about?visitor=1&uid=" + leavingmessage.getLmto();
        }
        return "main/about";
    }
}
